import {
  subdistrictgroupstats
} from '~/services/api/message'
import Toast from 'tdesign-miniprogram/toast/index';

Page({
  /** 
   * 页面的初始数据 
   */
  data: {
    goodsListLoadStatus: 0,
    pageLoading: false,
    loadMoreStatus: 0,
    agentKid: "",
    loading: true,
    vistorList: [],
    kid: '',
    pullDownRefreshing: false,
    emptyImg: 'https://cdn-we-retail.ym.tencent.com/miniapp/order/empty-order-list.png',
    userId: ''
  },
  goodListPagination: {
    pageNo: 1,
    pageSize: 10,
  },
  onLoad: function () {
    var that = this;
    var pages = getCurrentPages() //获取加载的页面
    var currentPage = pages[pages.length - 1] //获取当前页面的对象
    var options = currentPage.options //如果要获取url中所带的参数可以查看options
    this.setData({
      agentKid: options.kid
    })
    // this.getList();
  },
  toDetail(e) {
    wx.navigateTo({
      url: `/subpages/pages//addvisitorRecord/addvisitorRecord?isEdit=true&kid=${e.currentTarget.dataset.group.kid}&hideButton=true`,
    })
  },
  async getList() {
    const role = wx.getStorageSync('role');
    const userId = role?.userId;
    let params = {
      agentKid: this.data.agentKid
    };

    let {
      data
    } = await subdistrictgroupstats(params);
    this.setData({
      vistorList: data.entities
    })
  },
  add() {
    wx.navigateTo({
      url: '/subpages/pages/addvisitorRecord/addvisitorRecord?hideButton=false',
    })
  },
  onShow() {
    this.loadHomePage();
  },

  loadHomePage() {
    wx.stopPullDownRefresh();
    this.setData({
      pageLoading: true,
    });
    this.loadGoodsList(true);
  },
  onPullDownRefresh() {
    this.loadHomePage();
  },
  onReTry() {
    this.loadGoodsList();
  },
  async loadGoodsList(fresh = false) {
    const role = wx.getStorageSync('role');
    const userId = role?.userId;
    if (fresh) {
      wx.pageScrollTo({
        scrollTop: 0,
      });
      this.setData({
        pageLoading: false,
      });
    }
    this.setData({
      goodsListLoadStatus: 1
    });
    const pageSize = this.goodListPagination.pageSize;
    let pageIndex = this.goodListPagination.pageNo + 1;
    if (fresh) {
      pageIndex = 1;
    }
    try {
      const {
        data
      } = await subdistrictgroupstats({
        agentKid: this.data.agentKid
        // "pageNo": pageIndex,
        // "pageSize": pageSize,
        // "param": {
        //   userId: this.data.userId || userId
        // }
      });
      console.log(data, 'ddd');
      this.setData({
        vistorList: fresh ? data : this.data.vistorList.concat(data),
        goodsListLoadStatus: (pageIndex == 1 && data.length < 10) ? 2 : data.length > 1 ? 0 : 2,
      });

      this.goodListPagination.pageSize = data.pageSize;
      this.goodListPagination.pageNo = data.pageNo;
    } catch (err) {
      this.setData({
        goodsListLoadStatus: 3
      });
    }
  },
  navToTerminal(e) {
    let group = e.currentTarget.dataset.group;
    wx.navigateTo({
      url: `/subpages/pages/allTerminal/allTerminal?agentUserId=${this.data.agentKid}&title=${group.subdistrictName}意向客户&type=1`,
    })
  },

  navTodealTerminal(e) {
    let group = e.currentTarget.dataset.group;
    wx.navigateTo({
      url: `/subpages/pages/allTerminal/allTerminal?agentUserId=${this.data.agentKid}&title=${group.subdistrictName}成交客户&type=2`,
    })
  },

  onReachBottom() {
    if (this.data.goodsListLoadStatus === 0) {
      this.loadGoodsList();
    }
  },

})